package com.vehicle.controller;

import com.vehicle.bean.DataGridView;
import com.vehicle.bean.ResultDto;
import com.vehicle.pojo.Car;
import com.vehicle.pojo.Driver;
import com.vehicle.pojo.FeesRecord;
import com.vehicle.service.CarService;
import com.vehicle.service.FeesRecordService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/feesRecord")
public class FeesRecordController extends BaseController{
    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private FeesRecordService feesRecordService;
    @Autowired
    private CarService carService;


    @RequestMapping("/feesRecords")
    @ResponseBody
    public DataGridView findAllFeesRecord(FeesRecord feesRecord,Integer carid){
        Car car = new Car();
        car.setCarid(carid);
        feesRecord.setCar(car);
        return feesRecordService.findAllFeesRecord(feesRecord);
    }
    @RequestMapping("/deletefeesRecord")
    @ResponseBody
    public ResultDto deleteFeesRecord(Integer feesid) {
        logger.info(feesid);
        ResultDto resultDto = new ResultDto();
        try {
            feesRecordService.deleteFeesRecordByFeesId(feesid);
            resultDto.setCode(200);
            resultDto.setMsg("删除成功");
        } catch (Exception e) {
            logger.info("失败了");
            resultDto.setCode(-200);
            resultDto.setMsg("删除失败");
            e.printStackTrace();
        }
        return resultDto;
    }
    @RequestMapping(value = "/addFeesRecord")
    @ResponseBody
    public ResultDto addFeesRecord(FeesRecord feesRecord){
        ResultDto resultDto = new ResultDto();
        try {
            feesRecordService.addFeesRecord(feesRecord);
            resultDto.setMsg("增加成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setCode(-200);
            resultDto.setMsg("增加失败");
        }
        return resultDto;
    }
    @ResponseBody
    @RequestMapping(value = "/updateFeesRecord")
    public ResultDto updateFeesRecord(FeesRecord feesRecord){
        ResultDto resultDto = new ResultDto();
        try {
            feesRecordService.updateFeesRecord(feesRecord);
            resultDto.setCode(200);
            resultDto.setMsg("修改成功");
        } catch (Exception e) {
            resultDto.setMsg("修改失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }finally {
            return resultDto;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/deleteFeesRecords")
    public ResultDto deleteBatchByIds(@RequestParam("feesIds[]") Integer [] feesIds){
        ResultDto resultDto = new ResultDto();
        try {
            feesRecordService.deleteBatchFeesRecordByFeesIds(feesIds);
            resultDto.setMsg("删除成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("删除失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }finally {
            return resultDto;
        }
    }
}
